package com.sdp.irrigation.kafka.utils;

import java.util.LinkedHashMap;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.sdp.irrigation.entity.IrrigationEmp;
import com.sdp.irrigation.protocol.CommPackage;
import com.sdp.irrigation.utils.CommConstants;
import com.sdp.irrigation.utils.DownlinkCodeEnum;
import com.sdp.irrigation.utils.IrrigationConstants;
import com.sdp.irrigation.utils.ProtocolTypeEnum;
import com.sdp.irrigation.utils.ServerResponseEnum;
import com.sdp.irrigation.view.DownAgitationParam;
import com.sdp.irrigation.view.DownDesiltingParam;
import com.sdp.irrigation.view.DownEqCloseParam;
import com.sdp.irrigation.view.DownEqOpenParam;
import com.sdp.irrigation.view.DownFertigationParam;
import com.sdp.irrigation.view.DownSettleParam;
import com.sdp.irrigation.view.DownValveParam;
import com.sdp.irrigation.view.DownWaterSupplyParam;

public class ProtocolUtils {
	
	
	
	/**
	 * 获取关闭水泵下发报文
	 * @param downEqCloseParam
	 * @return
	 */
	public static CommPackage getDownEqCloseCommPackage(DownEqCloseParam downEqCloseParam) {
		CommPackage commPackage = new CommPackage();
		//commPackage.setPakSn(LocalDateUtil.localDateTime2String(LocalDateTime.now(), CommConstants.TIME_FORMATTER));
		commPackage.setDevSn(downEqCloseParam.getEqNo());
		commPackage.setCode(DownlinkCodeEnum.PUMP_OFF.getCode());
		JSONObject body = new JSONObject(new LinkedHashMap<>());
		body.put("cardNum", downEqCloseParam.getIcNo());
		commPackage.setBody(body);
		return commPackage;
	}
	
	/**
	 * 获取打开水泵下发报文
	 * @param downEqOpenParam
	 * @return
	 */
	public static CommPackage getDownEqOpenCommPackage(DownEqOpenParam downEqOpenParam) {
		CommPackage commPackage = new CommPackage();
		//commPackage.setPakSn(LocalDateUtil.localDateTime2String(LocalDateTime.now(), CommConstants.TIME_FORMATTER));
		commPackage.setDevSn(downEqOpenParam.getEqNo());
		commPackage.setCode(DownlinkCodeEnum.PUMP_ON.getCode());
		JSONObject body = new JSONObject(new LinkedHashMap<>());
		body.put("cardNum", downEqOpenParam.getIcNo());
		if (downEqOpenParam.getMoney() != null) {
			body.put("money", downEqOpenParam.getMoney());
		} else {
			body.put("money", 0);
		}
		if (downEqOpenParam.getTime() != null) {
			body.put("time", downEqOpenParam.getTime());
		}
		
		commPackage.setBody(body);
		return commPackage;
	}
	
	/**
	 * 获取施肥下发报文
	 * @param downFertigationParam
	 * @return
	 */
	public static CommPackage getDownFertigationCommPackage(DownFertigationParam downFertigationParam) {
		CommPackage commPackage = new CommPackage();
		//commPackage.setPakSn(LocalDateUtil.localDateTime2String(LocalDateTime.now(), CommConstants.TIME_FORMATTER));
		commPackage.setDevSn(downFertigationParam.getEqNo());
		commPackage.setCode(DownlinkCodeEnum.FERTIGATION.getCode());
		JSONObject body = new JSONObject(new LinkedHashMap<>());
		body.put("cardNum", downFertigationParam.getIcNo());
		if (downFertigationParam.getShifei() != null) {
			body.put("shifei", downFertigationParam.getShifei());
		}
		body.put("fg", downFertigationParam.getIsOpen());
		
		commPackage.setBody(body);
		return commPackage;
	}
	
	/**
	 * 获取搅拌下发报文
	 * @param downAgitationParam
	 * @return
	 */
	public static CommPackage getDownAgitationCommPackage(DownAgitationParam downAgitationParam) {
		CommPackage commPackage = new CommPackage();
		//commPackage.setPakSn(LocalDateUtil.localDateTime2String(LocalDateTime.now(), CommConstants.TIME_FORMATTER));
		commPackage.setDevSn(downAgitationParam.getEqNo());
		commPackage.setCode(DownlinkCodeEnum.AGITATION.getCode());
		JSONObject body = new JSONObject(new LinkedHashMap<>());
		body.put("cardNum", downAgitationParam.getIcNo());
		if (downAgitationParam.getJiaoban() != null) {
			body.put("jiaoban", downAgitationParam.getJiaoban());
		}
		body.put("fg", downAgitationParam.getIsOpen());
		
		commPackage.setBody(body);
		return commPackage;
	}
	
	/**
	 * 获取加水下发报文
	 * @param downWaterSupplyParam
	 * @return
	 */
	public static CommPackage getDownWaterSupplyCommPackage(DownWaterSupplyParam downWaterSupplyParam) {
		CommPackage commPackage = new CommPackage();
		//commPackage.setPakSn(LocalDateUtil.localDateTime2String(LocalDateTime.now(), CommConstants.TIME_FORMATTER));
		commPackage.setDevSn(downWaterSupplyParam.getEqNo());
		commPackage.setCode(DownlinkCodeEnum.WATER_SUPPLY.getCode());
		JSONObject body = new JSONObject(new LinkedHashMap<>());
		body.put("cardNum", downWaterSupplyParam.getIcNo());
		if (downWaterSupplyParam.getJiashui() != null) {
			body.put("jiashui", downWaterSupplyParam.getJiashui());
		}
		body.put("fg", downWaterSupplyParam.getIsOpen());
		
		commPackage.setBody(body);
		return commPackage;
	}
	
	/**
	 * 获取排沙下发报文
	 * @param downDesiltingParam
	 * @return
	 */
	public static CommPackage getDownDesiltingCommPackage(DownDesiltingParam downDesiltingParam) {
		CommPackage commPackage = new CommPackage();
		//commPackage.setPakSn(LocalDateUtil.localDateTime2String(LocalDateTime.now(), CommConstants.TIME_FORMATTER));
		commPackage.setDevSn(downDesiltingParam.getEqNo());
		commPackage.setCode(DownlinkCodeEnum.DESILTING.getCode());
		JSONObject body = new JSONObject(new LinkedHashMap<>());
		body.put("cardNum", downDesiltingParam.getIcNo());
		if (downDesiltingParam.getPaisha() != null) {
			body.put("paisha", downDesiltingParam.getPaisha());
		}
		body.put("fg", downDesiltingParam.getIsOpen());
		
		commPackage.setBody(body);
		return commPackage;
	}
	
	public static CommPackage getDownSettleCommPackage(DownSettleParam downSettleParam) {
		CommPackage commPackage = new CommPackage();
		commPackage.setPakSn(CommConstants.generatePakSn());
		commPackage.setDevSn(downSettleParam.getEqNo());
		commPackage.setCode(DownlinkCodeEnum.SETTLE.getCode());
		JSONObject body = new JSONObject(new LinkedHashMap<>());
		body.put("cardNum", downSettleParam.getIcNo());
		commPackage.setBody(body);
		return commPackage;
	}
	
	/**
	 * 获取开关阀门下发报文
	 * @param downValveParam
	 * @return
	 */
	public static CommPackage getDownValveCommPackage(DownValveParam downValveParam) {
		CommPackage commPackage = new CommPackage();
		//commPackage.setPakSn(CommConstants.generatePaksn());
		commPackage.setDevSn(downValveParam.getEqNo());
		commPackage.setCode(DownlinkCodeEnum.VALVE_ON_OFF.getCode());
		JSONObject body = new JSONObject(new LinkedHashMap<>());
		body.put("cardNum", downValveParam.getIcNo());
		StringBuilder sbf = new StringBuilder();
		sbf.append("000000");
		for (int i = 0; i < 26; i++) {
			if (i + 1 == Integer.parseInt(downValveParam.getValveNo())) {
				sbf.append(IrrigationConstants.是.getCode());
			} else {
				sbf.append(IrrigationConstants.否.getCode());
			}
		}
		sbf.reverse();
		body.put("valve", sbf.toString());
		body.put("fg", downValveParam.getIsOpen());
		
		commPackage.setBody(body);		
		return commPackage;
	}
	
	/**
	 * 获取上报设备帧下发报文
	 * @param devSn
	 * @return
	 */
	public static CommPackage getDownReportCommPackage(String devSn) {
		CommPackage commPackage = new CommPackage();
		commPackage.setPakSn(CommConstants.generatePakSn());
		commPackage.setDevSn(devSn);
		commPackage.setCode(null);
		commPackage.setType(ProtocolTypeEnum.REPORT.getCode());
		return commPackage;
	}
	
	public static CommPackage getDownBalanceResultCommPackage(String devSn, String icNo, Integer money, IrrigationEmp emp) {
		CommPackage commPackage = new CommPackage();
		commPackage.setPakSn(CommConstants.generatePakSn());
		commPackage.setDevSn(devSn);
		commPackage.setCode(ServerResponseEnum.BALANCE_RESULT.getCode());
		JSONObject body = new JSONObject(new LinkedHashMap<>());
		body.put("cardNum", icNo);
		body.put("money", money);
		body.put("time", 0);
		if (StringUtils.isEmpty(emp.getSj1())) {
			body.put("SJ1", "0");
		} else {
			body.put("SJ1", emp.getSj1());
		}
		if (StringUtils.isEmpty(emp.getSl1())) {
			body.put("SL1", "0");
		} else {
			body.put("SL1", emp.getSl1());
		}
		if (StringUtils.isEmpty(emp.getSj2())) {
			body.put("SJ2", "0");
		} else {
			body.put("SJ2", emp.getSj2());
		}
		if (StringUtils.isEmpty(emp.getSl2())) {
			body.put("SL2", "0");
		} else {
			body.put("SL2", emp.getSl2());
		}
		if (StringUtils.isEmpty(emp.getSj3())) {
			body.put("SJ3", "0");
		} else {
			body.put("SJ3", emp.getSj3());
		}
		if (StringUtils.isEmpty(emp.getSl3())) {
			body.put("SL3", "0");
		} else {
			body.put("SL3", emp.getSl3());
		}
		
		if (StringUtils.isEmpty(emp.getDj1())) {
			body.put("DJ1", "0");
		} else {
			body.put("DJ1", emp.getDj1());
		}
		if (StringUtils.isEmpty(emp.getDl1())) {
			body.put("DL1", "0");
		} else {
			body.put("DL1", emp.getDl1());
		}
		if (StringUtils.isEmpty(emp.getDj2())) {
			body.put("DJ2", "0");
		} else {
			body.put("DJ2", emp.getDj2());
		}
		if (StringUtils.isEmpty(emp.getDl2())) {
			body.put("DL2", "0");
		} else {
			body.put("DL2", emp.getDl2());
		}
		if (StringUtils.isEmpty(emp.getDj3())) {
			body.put("DJ3", "0");
		} else {
			body.put("DJ3", emp.getDj3());
		}
		if (StringUtils.isEmpty(emp.getDl3())) {
			body.put("DL3", "0");
		} else {
			body.put("DL3", emp.getDl3());
		}
		if (StringUtils.isEmpty(emp.getMianji())) {
			body.put("mianji", "0");
		} else {
			body.put("mianji", emp.getMianji());
		}
		commPackage.setBody(body);
		return commPackage;
	}
	
	public static void main(String[] args) {
		
//		CommPackage commPackage = new CommPackage();
//		commPackage.setPakSn(LocalDateUtil.localDateTime2String(LocalDateTime.now(), CommConstants.TIME_FORMATTER));
//		commPackage.setDevSn("123");
//		commPackage.setCode(null);
//		commPackage.setType(ProtocolTypeEnum.REPORT.getCode());
//		System.out.println(JsonUtils.toJSONString(commPackage));
		
//		CommPackage commPackage = new CommPackage();
//		commPackage.setPakSn(LocalDateUtil.localDateTime2String(LocalDateTime.now(), CommConstants.TIME_FORMATTER));
//		commPackage.setDevSn("123");
//		commPackage.setCode(DownlinkCodeEnum.VALVE_ON_OFF.getCode());
//		JSONObject body = new JSONObject(new LinkedHashMap<>());
//		body.put("cardNum", "456");
//		StringBuilder sbf = new StringBuilder();
//		sbf.append("000000");
//		for (int i = 0; i < 26; i++) {
//			if (i + 1 == Integer.parseInt("1")) {
//				sbf.append(IrrigationConstants.是.getCode());
//			} else {
//				sbf.append(IrrigationConstants.否.getCode());
//			}
//		}
//		sbf.reverse();
//		body.put("valve", sbf.toString());
//		body.put("fg", "0");
//		
//		commPackage.setBody(body);
//		System.out.println(JSON.toJSONString(commPackage));
//		
//		String text = JSON.toJSONString(commPackage);
//		System.out.println(JSON.toJSONString(JSON.parseObject(text, CommPackage.class, Feature.OrderedField)));
		
	}

	public static String getUpMessageTopic(String type, String topicPrefix) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(topicPrefix).append(type);
		return sbf.toString();
	}
	
	public static String getDownMessageTopic(String type, String topicPrefix) {
		StringBuffer sbf = new StringBuffer();
		sbf.append(topicPrefix).append(type);
		return sbf.toString();
	}
	
}
